package bakenote.xml;


import bakenote.core.Procedure;
import bakenote.core.Recipe;
import org.apache.pivot.collections.List;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.xml.Element;
import org.apache.pivot.xml.TextNode;
import org.apache.pivot.xml.XMLSerializer;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: lello
 * Date: 4/29/11
 * Time: 9:32 PM
 */
public class ProcedureXMLWriter {

    private ProcedureXMLWriter() {
        //No-op
    }

    public static void writeXML(Procedure procedure, File xmlFile) throws Exception {

        Element procedureElement = new Element("procedure");
        for (int i = 0; i < procedure.getElementalProcedureList().getLength(); i++) {
            Element stepElement = new Element("step");
            Element timeElement = new Element("time");
            TextNode textNode = new TextNode(procedure.getStep(i).getDuration().toString());
            timeElement.add(textNode);
            Element descriptionElement = new Element("description");
            textNode = new TextNode(procedure.getStep(i).getDescription());
            descriptionElement.add(textNode);
            stepElement.add(timeElement);
            stepElement.add(descriptionElement);
            procedureElement.add(stepElement);
        }

        XMLSerializer xmlSerializer = new XMLSerializer();
        // Write the procedure to a stringWriter for later indentation
        StringWriter stringWriter = new StringWriter();
        xmlSerializer.writeObject(procedureElement, stringWriter);

        // Apply indentation to the stringWriter
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter indentedStringWriter = new StringWriter();
        transformer.transform(new StreamSource(new StringReader(stringWriter.toString())), new StreamResult(indentedStringWriter));
        // Save the xml element to a file
        FileWriter fw = new FileWriter(xmlFile, false);
        fw.write(indentedStringWriter.toString());
        fw.flush();
        fw.close();
    }


    public static String getXML(Procedure procedure) {

        Element procedureElement = new Element("procedure");
        for (int i = 0; i < procedure.getElementalProcedureList().getLength(); i++) {
            Element stepElement = new Element("step");
            Element timeElement = new Element("time");
            TextNode textNode = new TextNode(procedure.getStep(i).getDuration().toString());
            timeElement.add(textNode);
            Element descriptionElement = new Element("description");
            textNode = new TextNode(procedure.getStep(i).getDescription());
            descriptionElement.add(textNode);
            stepElement.add(timeElement);
            stepElement.add(descriptionElement);
            procedureElement.add(stepElement);
        }

        XMLSerializer xmlSerializer = new XMLSerializer();
        // Write the procedure to a stringWriter for later indentation
        StringWriter stringWriter = new StringWriter();
        try {
            xmlSerializer.writeObject(procedureElement, stringWriter);
        } catch (SerializationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        // Apply indentation to the stringWriter
        Transformer transformer = null;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter indentedStringWriter = new StringWriter();
        try {
            transformer.transform(new StreamSource(new StringReader(stringWriter.toString())), new StreamResult(indentedStringWriter));
        } catch (TransformerException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        // Save the xml element to a file
        return indentedStringWriter.toString();
    }

}
